package databaseTransaction;
//MainFrame.java
import javax.swing.*;


import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.*;

public class OverideChooser extends JFrame implements ActionListener {
	private boolean isCancel;
	private Table table;
	private JButton continueBt;
	private JButton cancelBt;
	private JRadioButton overideRbt;
	private JRadioButton mergeRbt;
	private ButtonGroup chooser;
	private JPanel chooserPanel;
	private JPanel submitPanel;
	private JLabel descriptionLb;
	
	
	public OverideChooser(final Table table){	   
		super("Overide or Merge Table");
		this.table = table;
		this.isCancel = false;
		String description = "Tabel " + table.getName() + "already exist.\n Do you want to overide it?";
		descriptionLb = new JLabel(description);
		descriptionLb.setSize(description.length()*10, 60);

		// ChooserPanel setting
		String overideMsg = "Overide current table";
		overideRbt = new JRadioButton(overideMsg);
		overideRbt.setSelected(true);
		overideRbt.setSize(overideMsg.length()*20, 60);
		
		String mergeMsg = "Merge current table and overide persistent code";
		mergeRbt = new JRadioButton(mergeMsg);
		mergeRbt.setSelected(false);
		mergeRbt.setSize(mergeMsg.length() * 10, 60);
		
		chooser = new ButtonGroup();
		chooser.add(overideRbt);
		chooser.add(mergeRbt);
		
		chooserPanel = new JPanel(new GridLayout(3, 1));
		chooserPanel.add(descriptionLb);
		chooserPanel.add(overideRbt);
		chooserPanel.add(mergeRbt);
		int chooserHeight = overideRbt.getHeight() + mergeRbt.getHeight();
		int chooserWidth = (overideRbt.getWidth() > mergeRbt.getWidth()) ? overideRbt.getWidth() : mergeRbt.getWidth();
		chooserPanel.setSize(chooserWidth, chooserHeight);
		
		//SubmitPanel Settings
		continueBt=new JButton("Continue");
		continueBt.setSize(100, 40);
		continueBt.addActionListener(this);
       
		cancelBt=new JButton("Cancel");
		cancelBt.setSize(100,40);
		cancelBt.addActionListener(this);
		
		submitPanel = new JPanel(new FlowLayout());
		submitPanel.add(continueBt);
		submitPanel.add(cancelBt);
		submitPanel.setSize(200, 60);

		//adding controls to JFrame

		getContentPane().add(chooserPanel);
		getContentPane().add(submitPanel);
		
		//settings for JFrame
		int frameHeight = chooserPanel.getHeight() + submitPanel.getHeight() + descriptionLb.getHeight();
		System.out.println(chooserPanel.getHeight() + "\n" + submitPanel.getHeight() + "\n" + descriptionLb.getHeight());
		int frameWidth = (chooserPanel.getWidth() > descriptionLb.getWidth()) ? chooserPanel.getWidth() : descriptionLb.getWidth();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setSize(frameWidth, frameHeight);
		getContentPane().setLayout(new GridLayout(2, 1));
		this.show();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		if (e.getSource() == this.continueBt){
			if (overideRbt.isSelected()){
				table.setOveride(true);
			}	
			if (mergeRbt.isSelected()){
				table.setOveride(false);
			}	
			this.isCancel = false;
			this.dispose();
		}
			
		else if (e.getSource() == this.cancelBt){
			this.isCancel = true;
			this.dispose();
		}	
	}
	
	public boolean isCancel(){
		return this.isCancel;
	}


} 

